﻿using Fiddler;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ProgramCat.FiddlerExtension
{
    public class GenerateCode_JyVisit : GenerateCodeBase
    {
        public override string GenerateCode( int compilingMode, Session session)
        {
            StringBuilder stringBuilder = new StringBuilder();
            stringBuilder.Append(".版本 2\r\n\r\n");
            stringBuilder.AppendLine(".子程序 功能_网页访问, 文本型, , 本子程序由编程猫内部插件生成,请配合精易模块使用。QQ群:747776374");
            stringBuilder.AppendLine(".局部变量 局_网址, 文本型");
            stringBuilder.AppendLine(".局部变量 局_提交数据, 文本型");
            stringBuilder.AppendLine(".局部变量 局_提交cookie, 文本型");
            if (compilingMode==0)
            {
                stringBuilder.AppendLine(".局部变量 ADD_协议头, 类_POST数据类");
            }
            stringBuilder.AppendLine(".局部变量 局_提交协议头, 文本型");
            stringBuilder.AppendLine(".局部变量 局_结果, 字节集");
            stringBuilder.AppendLine(".局部变量 局_返回, 文本型");
            string fullUrl = session.fullUrl;
            stringBuilder.AppendLine(string.Format("局_网址=\"{0}\"", fullUrl));

            int intRequestMethod =  (int)Enum.Parse(typeof(RequestMethod), session.RequestMethod, true);

            string body = System.Text.Encoding.UTF8.GetString(session.RequestBody);
            if (body!="")
            {
                body = body.Replace("\"", "\" + #引号 + \"");
                stringBuilder.AppendLine(string.Format("局_提交数据 = \"{0}\"", body));
            }
            GenerateCodeHelp.GenerateHeadAndCookies_Jy(session,ref stringBuilder, compilingMode);

            string post =  string.Format("局_结果 ＝  网页_访问 (局_网址, {0}, 局_提交数据, 局_提交Cookie, , 局_提交协议头, , , , , )", intRequestMethod);
            stringBuilder.AppendLine(post);

            stringBuilder.AppendLine("局_返回 = 编码_Utf8到Ansi（局_结果）'自己去找编码");
            stringBuilder.AppendLine("返回(局_返回)");
            

            return stringBuilder.ToString();
        }

        private enum RequestMethod
        {
            GET = 0,
            POST = 1,
            HEAD = 2,
            PUT = 3,
            OPTIONS = 4,
            DELETE = 5,
            TRACE = 6,
            CONNECT = 7
        }
    }
}
